# A makefile

CC=gcc
SVN_INC=-I$(SVN_HOME)/include `apr-1-config --includes` -I$(SVN_HOME)/include/subversion-1
CPPFLAGS=-DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -fPIC
CFLAGS=-pthread -g -Wall
APR_LIBS=`apr-1-config --libs --ldflags`
SVN_LIBS=-lsvn_repos-1 -lsvn_fs-1 -lsvn_subr-1 -lapr-1 -lsvn_delta-1
LIB_PATH=-L$(SVN_HOME)/lib

LD_FLAGS:=-shared -lc $(LIB_PATH) $(APR_LIBS) $(SVN_LIBS)
ifeq ($(shell uname), Darwin)
LD_FLAGS:=$(LD_FLAGS) -dylib  -arch x86_64
SO_EXT=dylib
endif
ifeq ($(shell uname), Linux)
LD_FLAGS:=$(LD_FLAGS) -shared -g
SO_EXT=so
endif

INCDIR=include

CLASSES=../web/WEB-INF/classes
CLASSPATH=$(CLASSES):../web/WEB-INF/lib/stax-utils.jar

JAVAH=$(JAVA_HOME)/bin/javah
JAVA=$(JAVA_HOME)/bin/java

JAVAH_FLAGS=-classpath $(CLASSES) -d $(INCDIR)
JAVA_FLAGS=-classpath $(CLASSES) -Djava.library.path=.

SETUID_LIB=libwdsetuid.$(SO_EXT)
OBJS=editor-driver.o repo-context.o jniutil.o native-editor.o noop-editor.o
EDITOR_LIB=libsvnj.$(SO_EXT)

COMPILE=$(CC) $(CPPFLAGS) $(CFLAGS) -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux -I$(INCDIR) $(SVN_INC)


.c.o:
	$(COMPILE) -o $@ -c $<

# We could be a bit smarter about the below
editor-driver.o: com_googlecode_svnj_EditorDriver.h

com_googlecode_svnj_EditorDriver.h:
	$(JAVAH) $(JAVAH_FLAGS) com.googlecode.svnj.EditorDriver

repo-context.o: com_googlecode_svnj_RepoContext.h

com_googlecode_svnj_RepoContext.h:
	$(JAVAH) $(JAVAH_FLAGS) com.googlecode.svnj.RepoContext

native-editor.o: com_googlecode_svnj_NativeEditor.h

com_googlecode_svnj_NativeEditor.h:
	$(JAVAH) $(JAVAH_FLAGS) com.googlecode.svnj.NativeEditor

all: editor-test

editor-test:
	$(CC) $(CPPFLAGS) $(CFLAGS) $(SVN_INC) $(LIB_PATH) $(SVN_LIBS) editor-test.c -o editor-test

native: native-setuid native-svnj

native-setuid:
	mkdir -p $(INCDIR)
	$(JAVAH) $(JAVAH_FLAGS) com.googlecode.svnj.setuid.Setuid
	$(COMPILE) -shared -lc wd_setuid.c -o $(SETUID_LIB)

native-svnj: $(OBJS)
	$(CC) $(LD_FLAGS) $(OBJS) -o $(EDITOR_LIB)

clean:
	rm -rf $(INCDIR)
	rm -f $(SETUID_LIB) $(EDITOR_LIB) editor-test $(OBJS)

FORCE:
